package com.jiuyuejiu.httpid;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.R;
import com.jiuyuejiu.application.ConstValues;
import com.jiuyuejiu.base.BaseActivity;
import com.jiuyuejiu.utils.ShanHaiUtil;
import com.kylin.core.utils.dbtutil.PrefUtils;
import com.kylin.core.utils.flyn.Eyes;

import java.lang.ref.SoftReference;

import androidx.appcompat.widget.AppCompatTextView;

/**
 *
 */
public class HttpipActivity extends BaseActivity implements View.OnClickListener {

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            // .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_gray_5)// 占位图
            .transform(new CenterCrop(), new RoundedCorners(18));

    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;
    private TextView httpipTv;
    private Button remoteBtn;
    private Button thinkpadBtn;
    private Button pcBtn;
    private EditText editEt;
    private Button editBtn;

    private String username;
    private String password;

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public class MyHandler extends Handler {

        // 软引用
        SoftReference<HttpipActivity> fragmentRef;

        public MyHandler(HttpipActivity fragment) {
            fragmentRef = new SoftReference<HttpipActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            HttpipActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }

            Bundle bundle = msg.getData();
            super.handleMessage(msg);
            // 处理UI 变化
            switch (msg.what) {
                // 提示信息
                case ConstValues.WAIT0:
                    //
                    // fragment.getPicSuc(bundle);
                    break;
                case ConstValues.WAIT1:
                    // 关闭缓冲界面
                    // LatteLoader.stopLoading();
                    break;

                default:
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_httpip);

        // 标题栏白底黑字
        Eyes.setStatusBarLightMode(this, Color.WHITE);

        // 接收传递过来的参数
        initCreate();
        // 初始化视图
        initView();
        // 初始化数据
        initData();
    }

    private void initCreate() {

        /*// 上一页面设置数据
        Intent intent = null;
        intent = new Intent(this, RePwdActivity.class);
        Bundle bundl = new Bundle();
        intent.putExtra("username", "");
        intent.putExtra("password", "");
        startActivity(intent);*/

        // 本页面获取数据
        Intent i = getIntent();
        username = (String) i.getSerializableExtra("username");
        password = (String) i.getSerializableExtra("password");
    }

    private void initView() {

        backBtn = (RelativeLayout) findViewById(R.id.top_navigation_rl_back);
        confirmBtn = (RelativeLayout) findViewById(R.id.top_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) findViewById(R.id.top_navigation_bt_confirm);
        backTv = (AppCompatTextView) findViewById(R.id.top_navigation_bt_back);
        titleTv = (AppCompatTextView) findViewById(R.id.top_navigation_tv_title);

        confirmBtn.setVisibility(View.INVISIBLE);
        //confirmBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);

        httpipTv = (TextView) findViewById(R.id.httpip_tv_current);
        remoteBtn = (Button) findViewById(R.id.httpip_btn_remote);
        thinkpadBtn = (Button) findViewById(R.id.httpip_btn_thinkpad);
        pcBtn = (Button) findViewById(R.id.httpip_btn_pc);
        editEt = (EditText) findViewById(R.id.httpip_et_edit);
        editBtn = (Button) findViewById(R.id.httpip_btn_edit);

        remoteBtn.setOnClickListener(this);
        thinkpadBtn.setOnClickListener(this);
        pcBtn.setOnClickListener(this);
        editBtn.setOnClickListener(this);

    }

    private void initData() {
        handler = new MyHandler(this);
        titleTv.setText("选择服务器ip");


        // 根据保存的ip 设置显示用户名,默认Remote
        httpipTv.setText(ShanHaiUtil.getHttpid());

        remoteBtn.setText("remote: "+ConstValues.HTTPHEAD_REMOTE);
        thinkpadBtn.setText("thinkpad: "+ConstValues.HTTPHEAD_THINKPAD);
        pcBtn.setText("pc: "+ConstValues.HTTPHEAD_PC);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            // 返回
            case R.id.top_navigation_rl_back:
                this.finish();
                break;
            case R.id.top_navigation_rl_confirm://
                break;

            case R.id.httpip_btn_remote://
                httpipTv.setText(ConstValues.HTTPHEAD_REMOTE);
                PrefUtils.putString(HttpipActivity.this, ConstValues.HTTPSERVICE, ConstValues.HTTPHEAD_REMOTE);
                showHeadPhoto();
                break;
            case R.id.httpip_btn_thinkpad://
                httpipTv.setText(ConstValues.HTTPHEAD_THINKPAD);
                PrefUtils.putString(HttpipActivity.this, ConstValues.HTTPSERVICE, ConstValues.HTTPHEAD_THINKPAD);
                showHeadPhoto();
                break;
            case R.id.httpip_btn_pc://
                httpipTv.setText(ConstValues.HTTPHEAD_PC);
                PrefUtils.putString(HttpipActivity.this, ConstValues.HTTPSERVICE, ConstValues.HTTPHEAD_PC);
                showHeadPhoto();
                break;
            case R.id.httpip_btn_edit://
                String ip = editEt.getText().toString();
                httpipTv.setText(ip);
                PrefUtils.putString(HttpipActivity.this, ConstValues.HTTPSERVICE, ip);
                showHeadPhoto();
                break;
        }
    }

    // 展示头像
    private void showHeadPhoto(){

        Dialog dialog = new Dialog(HttpipActivity.this, R.style.edit_AlertDialog_style);
        dialog.setContentView(R.layout.dialog_head_bigimg);
        ImageView imageView = (ImageView) dialog.findViewById(R.id.my_image);

        //选择true的话点击其他地方可以使dialog消失，为false的话不会消失
        dialog.setCanceledOnTouchOutside(true);

        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.x = 0;
        lp.y = 40;
        dialog.onWindowAttributesChanged(lp);

        Glide.with(HttpipActivity.this)
                .load(ConstValues.HEADPHOTO)
                .apply(BANNER_OPTIONS)
                .into(imageView);

        //大图的点击事件（点击让他消失）
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    @Override
    public void onBackPressedSupport() {
        // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
        super.onBackPressedSupport();
        this.finish();
    }
}
